// Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
// Distributable under LGPL license. See terms of license at gnu.org.

package nl.justobjects.pushlet.test;

import java.applet.Applet;
import java.awt.TextArea;
import java.awt.GridLayout;
import java.awt.Color;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.client.JavaPushletClient;
import nl.justobjects.pushlet.client.JavaPushletClientListener;

/**
 * Tester for applet clients; displays incoming events in text area.
 *
 * @version $Id: PushletApplet.java,v 1.8 2003/08/17 21:06:37 justb Exp $
 * @author Just van den Broecke - Just Objects &copy;
 **/
public class PushletApplet extends Applet implements JavaPushletClientListener {
  	private TextArea textArea=new TextArea(15, 40);
    private String url=null;
  	private JavaPushletClient javaPushletClient;

    public PushletApplet() {
    }

	/** Setup AWT stuff. */
	public void init() {
		p("PushletApplet - version 17.aug.2003-1");

		String subject = getParameter("subject");
		String pushletURI = getParameter("pushletURI");
		if (pushletURI == null){
			pushletURI= "/servlet/pushlet";
		}
		String host = getDocumentBase().getHost();
		int port = getDocumentBase().getPort();

		// Hmm sometimes this value is -1...(Moziila with Java 1.3.0 on Win)
		if (port == -1) {
			port = 80;
		}

		url = "http://" + host + ":" + port + pushletURI + "?format=xml&subject="+subject;

	    setLayout(new GridLayout(1,1));
	    textArea.setForeground(Color.yellow);
	    textArea.setBackground(Color.gray);
	  	textArea.setEditable(false);
	  	add(textArea);
    }

	public void start() {
		dbg("start()");
		if (javaPushletClient != null) {
			p("Stopping oldPushletClient");
			javaPushletClient.stop();
		}

	  	javaPushletClient = new JavaPushletClient();
		p("Starting PushletClient url=" + url);
		javaPushletClient.start(this, url);
	}

    public void stop() {
		dbg("stop()");
		if (javaPushletClient != null) {
			p("Stopping PushletClient");
			javaPushletClient.stop();
			javaPushletClient = null;
		}
    }

	/** Server has pushed probe event. */
    public void onProbe() {
    	p("<probe/>");
    }

	/** Server has pushed event. */
    public void onEvent(Event theEvent) {
    	p(theEvent.toXML());
    }

    /** Error occurred. */
    public void onError(String message) {
    	p(message);
    }

    /** Generic print. */
	public void p(String s) {
		dbg("event: "+s);
		synchronized (textArea) {
			textArea.append(s+"\n");
		}
	}

   /** Generic print. */
	public void dbg(String s) {
		System.out.println("[PushletApplet] " + s);
	}
}

 /*
  * $Log: PushletApplet.java,v $
  * Revision 1.8  2003/08/17 21:06:37  justb
  * version info change
  *
  * Revision 1.7  2003/08/15 08:37:41  justb
  * fix/add Copyright+LGPL file headers and footers
  *
  * Revision 1.6  2003/08/14 21:43:10  justb
  * improved Java client lifecycle; notably stopping listener thread
  *
  * Revision 1.5  2003/08/13 14:00:00  justb
  * some testing for applets; no real change
  *
  * Revision 1.4  2003/05/19 22:53:33  justb
  * more fixes for applets
  *
  * Revision 1.3  2003/05/19 21:56:29  justb
  * various fixes for applet clients
  *
  * Revision 1.2  2003/05/18 16:15:08  justb
  * support for XML encoded Events
  *
  * Revision 1.1.1.1  2002/09/24 21:02:33  justb
  * import to sourceforge
  *
  */
